<?php

  //Include database functions and connection
  include('inc/database.php');

  //Find ID from url
  $id = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], "/", 8) + 1);
  //Check ID is numeric
  if (!ctype_digit($id)) {
    showTextImage('Invalid ID');
    die();
  }

  //Grab the screenshot from the database
  $sql  = 'SELECT addon_screenshot FROM addons WHERE addon_id = \''.m($id).'\'';
  $res  = mysql_query($sql) or die(mysql_error());

  //Check there's one result and grab the result
  if (mysql_num_rows($res) != 1) {
    showTextImage('Invalid ID');
    die();
  } else {
    $row = mysql_fetch_assoc($res);
  }

  //Check if the screenshot is a screenshot and show
  if ($row['addon_screenshot'] == '') {
    showTextImage('No Image');
  } else {
    header("Content-Type: image/png");
    echo $row['addon_screenshot'];
  }

  /**
  * Creates an image with the specified text and outputs it.
  * 
  * @param text Text to display on the image
  */
  function showTextImage($text) {
    header("Content-Type: image/png");
    $im  = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc  = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    imagestring($im, 2, 5, 5, $text, $tc);
    imagepng($im);
    imagedestroy($im);
  }
?>
